//
//  BillAPI.swift
//  kano
//
//  Created by Frank on 2024/9/19.
//

import Foundation
import Moya
import KakaJSON

enum BillAPI {
    case billList(_ parameters: Dictionary<String, Any>? = nil)
    case billDetail(_ parameters: Dictionary<String, Any>? = nil)
    case repayHistory(_ parameters: Dictionary<String, Any>? = nil)
    case valist(_ parameters: Dictionary<String, Any>? = nil)
    case userInfo(_ parameters: Dictionary<String, Any>? = nil)
    case userBasic(_ parameters: Dictionary<String, Any>? = nil)
    case penalty(_ parameters: Dictionary<String, Any>? = nil)
    case loanAgreement(_ parameters: Dictionary<String, Any>? = nil)
    case repaymentRecord(_ parameters: Dictionary<String, Any>? = nil)
}

extension BillAPI : TargetType {
    var baseURL: URL {
        return URL(string: NetConfig.readBaseUrl())!
    }
    
    var path: String {
        switch self {
        case .billList:
            let path = NetConfig.fullNormal(path: "/overdueFeePolicy")
            return path
        case .billDetail:
            let path = NetConfig.fullNormal(path: "/loanApplicationFeedback")
            return path
        case .repayHistory:
            let path = NetConfig.fullNormal(path: "/loanPurposeSelection")
            return path
        case .valist:
            let path = NetConfig.fullNormal(path: "/repayAssessment")
            return path
        case .userInfo:
            let path = NetConfig.fullNormal(path: "/userExtendedDetails")
            return path
        case .userBasic:
            let path = NetConfig.fullNormal(path: "/userBasicInformation")
            return path
        case .penalty:
            let path = NetConfig.fullNormal(path: "/loanApplicationSubmission")
            return path
        case .loanAgreement:
            let path = NetConfig.fullNormal(path: "/addAccountFeedback")
            return path
        case .repaymentRecord:
            let path = NetConfig.fullNormal(path: "/loanPurposeSelection")
            return path
        default:
            return ""
        }
    }
    
    var method: Moya.Method {
        switch self {
        default:
            return .post
        }
    }
    
    var task: Moya.Task {
        switch self {
        case .billList(let parameters): fallthrough
        case .billDetail(let parameters): fallthrough
        case .valist(let parameters): fallthrough
        case .userInfo(let parameters): fallthrough
        case .userBasic(let parameters): fallthrough
        case .penalty(let parameters): fallthrough
        case .loanAgreement(let parameters): fallthrough
        case .repaymentRecord(let parameters): fallthrough
        case .repayHistory(let parameters):
            if kisEncrypt == 1 {
                let data: Dictionary<String, Any>
                if (parameters == nil) {
                    data = ["body" : ""]
                } else {
                    let bodyData = KAEncrypt.ka_encrypt(parameters!.jsonString()!)
                    data = ["body" : bodyData]
                }
                return .requestCompositeParameters(bodyParameters: data as [String : Any], bodyEncoding: JSONEncoding.default, urlParameters: NetConfig.urlParam())
            } else {
                return .requestCompositeParameters(bodyParameters:parameters ?? ["" : ""], bodyEncoding: JSONEncoding.default, urlParameters: NetConfig.urlParam())
            }
        default:
            return .requestPlain
        }
    }
    
    var headers: [String : String]? {
        return ["Content-Type":"application/json; charset=utf-8",
                "SW-K-ID": UIDevice.ka_UUID.toBase64()]
    }
}




